
package com.insigma.business.qrcode.merchant._provider.impl;


import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.merchant.dto.search.CareWelfareMerchantStreetRelBSearcher;
import com.insigma.business.qrcode.welfare.dto.CareWelfareMerchantStreetRelDTO;
import com.insigma.business.qrcode.merchant.entity.CareWelfareMerchantStreetRelDO;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantStreetRelInsertRequest;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantStreetRelSearchRequest;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantStreetRelUpdateRequest;
import com.insigma.business.qrcode.merchant._provider.CareWelfareMerchantStreetRelProvider;
import com.insigma.business.qrcode.merchant.service.CareWelfareMerchantStreetRelService;
import com.insigma.business.qrcode.common.converter.CareWelfareMerchantStreetRelProviderConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("careWelfareMerchantStreetRelProvider")
public class CareWelfareMerchantStreetRelProviderImpl extends BaseProviderImpl implements CareWelfareMerchantStreetRelProvider {

    @Autowired
    private CareWelfareMerchantStreetRelService careWelfareMerchantStreetRelService;


    @Override
    public BasePagingResponse<List<CareWelfareMerchantStreetRelDTO>> searchCareWelfareMerchantStreetRel(final CareWelfareMerchantStreetRelSearchRequest searchRequest) {

        final BasePagingResponse<List<CareWelfareMerchantStreetRelDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CareWelfareMerchantStreetRel的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CareWelfareMerchantStreetRelBSearcher careWelfareMerchantStreetRelBSearcher = CareWelfareMerchantStreetRelProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<CareWelfareMerchantStreetRelDO> careWelfareMerchantStreetRelDOList = careWelfareMerchantStreetRelService.search(careWelfareMerchantStreetRelBSearcher);
                final List<CareWelfareMerchantStreetRelDTO> careWelfareMerchantStreetRelDTOList = CareWelfareMerchantStreetRelProviderConverter.convertResponseDOList2DTOList(careWelfareMerchantStreetRelDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, careWelfareMerchantStreetRelDTOList, searchRequest,careWelfareMerchantStreetRelBSearcher.getTotalCount());
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_STREET_REL_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<CareWelfareMerchantStreetRelDTO> getCareWelfareMerchantStreetRel(final BaseGetRequest baseGetRequest){

        final BaseResponse<CareWelfareMerchantStreetRelDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CareWelfareMerchantStreetRel的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CareWelfareMerchantStreetRelDO careWelfareMerchantStreetRelDO = careWelfareMerchantStreetRelService.get((String)baseGetRequest.getId());
                final CareWelfareMerchantStreetRelDTO remoteObj = CareWelfareMerchantStreetRelProviderConverter.convertResponseDO2DTO(careWelfareMerchantStreetRelDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_STREET_REL_GET;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<String> insertCareWelfareMerchantStreetRel(final CareWelfareMerchantStreetRelInsertRequest insertRequest){

        BizCheckUtil.checkNotNull(insertRequest, "新增CareWelfareMerchantStreetRel的请求参数不能为空");

        CareWelfareMerchantStreetRelDO byStreetIdAndMerchantId = careWelfareMerchantStreetRelService.getByStreetIdAndMerchantId(insertRequest.getStreetId(), insertRequest.getMerchantId());
        BizCheckUtil.checkIsNull(byStreetIdAndMerchantId, "已添加");

        final CareWelfareMerchantStreetRelDO careWelfareMerchantStreetRelDO = CareWelfareMerchantStreetRelProviderConverter.convertRequestIO2DO(insertRequest);
        careWelfareMerchantStreetRelService.insert(careWelfareMerchantStreetRelDO);

        return ResponseUtils.buildSuccessResponse(careWelfareMerchantStreetRelDO.getId());
    }


    @Override
    public BaseResponse<Integer> updateCareWelfareMerchantStreetRel(final CareWelfareMerchantStreetRelUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CareWelfareMerchantStreetRel的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                CareWelfareMerchantStreetRelDO careWelfareMerchantStreetRelDO = CareWelfareMerchantStreetRelProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = careWelfareMerchantStreetRelService.update(careWelfareMerchantStreetRelDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_STREET_REL_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> deleteCareWelfareMerchantStreetRel(CareWelfareMerchantStreetRelUpdateRequest deleteRequest) {


        CareWelfareMerchantStreetRelDO byStreetIdAndMerchantId = careWelfareMerchantStreetRelService.getByStreetIdAndMerchantId(deleteRequest.getStreetId(), deleteRequest.getMerchantId());
        if (byStreetIdAndMerchantId != null) {
            careWelfareMerchantStreetRelService.delete(byStreetIdAndMerchantId.getId(), deleteRequest.getModifierType(), deleteRequest.getModifierId());
        }
        return ResponseUtils.buildSuccessResponse(0);
    }


    @Override
    public BaseResponse<Integer> deleteCareWelfareMerchantStreetRel(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CareWelfareMerchantStreetRel的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = careWelfareMerchantStreetRelService.delete((String)deleteRequest.getId(),deleteRequest.getModifierType(),(String)deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.CARE_WELFARE_MERCHANT_STREET_REL_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }
}
